Skip to content

Fix race condition in table initialization with multiple threads#243

Merged
aous72 merged 1 commit intoaous72:masterfrom
brechtvl:fix-race-condition
Feb 26, 2026
Merged

Fix race condition in table initialization with multiple threads#243
aous72 merged 1 commit intoaous72:masterfrom
brechtvl:fix-race-condition

Conversation

@brechtvl
Copy link
Contributor

@brechtvl brechtvl commented Jan 27, 2026

This caused intermittent wrong encoding in the OpenEXR integration, which then resulted in "Error decoding a codeblock" while decoding.

Use std::call_once to resolve this.

This caused wrong encoding in the OpenEXR integration, which then resulted
in "Error decoding a codeblock" while decoding.

Use std::call_once to avoid this.

Signed-off-by: Brecht Van Lommel <brecht@blender.org>
@brechtvl
Copy link
Contributor Author

brechtvl commented Jan 27, 2026

This issue was found integrating the new OpenEXR HTJ2K encoding in Blender, and I confirmed that a randomly failing test now reliably passes.

The bug was found and fixed by Gemini CLI, but I did review the fix carefully. A program to reproduce the issue using thread sanitizer was also generated, I've attached that. The fix resolves all thread sanitizer warnings in that program. I didn't see a way to reproduce this with existing programs included with OpenJPH.
race_condition_repro.zip

Potentially related issues:
AcademySoftwareFoundation/openexr#2207
#223

@aous72
Copy link
Owner

aous72 commented Jan 27, 2026

Dear Brecht,

Thank you for this important discovery and for the PR.

I am well with the suggested changes, but I am allergic to opaque C++ code.
I would like to use std::atomic_flag or std::atomic_bool for the initialization flag, and a simple std::atomic_flag lock to protect the critical section (the initialization section), if that is alright with you. Still not very clear on how to do that.

I am happy to do the changes if that is alright with you.

Thanx again.

Kind regards,
Aous.

@brechtvl
Copy link
Contributor Author

Thanks for the quickly reply. Feel free to make changes.

@aous72
Copy link
Owner

aous72 commented Feb 26, 2026

I wanted to make changes, but I am short on time.
I think for saving me time and for fixing these issues.

@aous72 aous72 merged commit 43e4715 into aous72:master Feb 26, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants